<?php
session_start();
require('class/auth.php');
include_once('class/autoload.php'); // pour inclure nos classes

if(Auth::islog()){
}
else
{
	header('Location:index.php');
}
$site = new page_securiser('acceuil');

$site->titre = "Acceuil";
$site->corps='
				<nav = align="center">
				<input type="button" value="Rechercher une annonce" onclick="location.href=\'acceuil.php\';">
				<input type="button" value="Deposer une annonce" onclick="location.href=\'DeposerAppartement.php\';">
				</nav>';


$site->affiche();


global $connexion;
$requete = 'select typeA from typeappart';
$TypeHabitation = $connexion->query($requete);

$requete1 = 'select typeH from typehabitation where idTypeH != 1';
$Nbpiece = $connexion->query($requete1);

if(!empty($_POST) )
{	
	$adresse = addslashes($_POST['adresse']);
	$ville = addslashes($_POST['ville']);
	$codepostale = addslashes($_POST['codepostal']);
	$description_envirronement = addslashes($_POST['description_envirronement']);
	$typeH =  addslashes($_POST['typeH']);
	$typeA = addslashes($_POST['typeA']);
	$piece = addslashes($_POST['piece']);
	$surface =  addslashes($_POST['surface']);
	$prix = addslashes($_POST['prix']);
	$meubler = addslashes($_POST['meubler']);
	$distance = addslashes($_POST['distance']);
	$Description = addslashes($_POST['Description']);
	
	if (is_numeric($_POST['prix'])) {
		$q = array('adresse' => $adresse, 'ville'=>$ville, 'codepostal'=>$codepostale, 'description_envirronement'=>$description_envirronement, 'typeH'=>$typeH, 'typeA'=>$typeA,
		'piece'=>$piece, 'surface'=>$surface, 'prix'=>$prix, 'meubler'=>$meubler, 'distance'=>$distance, 'Description'=>$Description );
			$sql = 'INSERT INTO appartement (libelleAppart, adRue, adCP, adVille, surface, distanceLycee, prixLoyer, meuble, description, nbPiece, idTypeH, idTypeA ) VALUES (:description_envirronement, :adresse, :codepostal, :ville, :surface, :distance, :prix, :meubler, :Description, :piece, :typeH, :typeA)';
			$req = $connexion->prepare($sql);
			$req->execute($q);
			$req->closeCursor();
			
			$sql = 'SELECT idAppart FROM appartement WHERE description = "'.$Description.'" AND libelleAppart = "'.$description_envirronement.'";';
			$result1 = $connexion->query($sql);
			$result1->setFetchMode(PDO::FETCH_OBJ);
			while($data = $resul1t->fetch()) {
				$idA = $data->idAppart;
			}
			$result1->closeCursor();
			
			//image
			$allowedExts = array("jpeg", "jpg");
			$temp = explode(".", $_FILES["file"]["name"]);
			$extension = end($temp);
			$nbimg = 1;
			if (is_uploaded_file($_FILES['file']['tmp_name']))
			{
				foreach($_FILES['file']['tmp_name'] as $key => $tmp_name)
				{
					if ($nbimg <= 3)
					{
						if (($_FILES["file"]["type"][$key] == "image/jpeg") || ($_FILES["file"]["type"][$key] == "image/jpg") || ($_FILES["file"]["type"][$key] == "image/pjpeg") && ($_FILES["file"]["size"][$key] < 200000) && in_array($extension, $allowedExts))
						{
							if ($_FILES["file"]["error"][$key] > 0)
							{
								echo "Return Code: " . $_FILES["file"]["error"][$key] . "<br>";
							}
							else
							{
								$requete = 'SELECT id FROM eleve WHERE mail = "'.$mail.'";';
								$result = $connexion->query($requete);
								$result->setFetchMode(PDO::FETCH_OBJ);
								while($data = $result->fetch()) {
									$id = $data->id;
								}
								$result->closeCursor();
			
								$urlimg = $id."_0".$nbimg;
								move_uploaded_file($_FILES["file"]["tmp_name"][$key],"images/avatar_eleve/".$id."_0".$nbimg.".".pathinfo($_FILES["file"]["name"][$key], PATHINFO_EXTENSION));
								$qimg = array('idA' => $idA, 'urlimg' => $urlimg);
								$sqlimg = 'INSERT INTO image (idAppart, idImage, url) VALUES (:idA,  , :urlimg)';
								$reqimg = $connexion->prepare($sqlimg);
								$reqimg->execute($qimg);
								$reqimg->closeCursor();
							}
						}
						else
						{
							?>
									<script type='text/javascript'>
									alert("Le fichier n'est pas valide, veuillez choisir un .jpg");
									</script>
									<?php
							}
							$nbimg = $nbimg + 1;
						}
					}
				}
				else
				{
					$qnoimg = array('idA' => $idA);
					$sqlnoimg = 'INSERT INTO image (idAppart, idImage, url) VALUES (:idA,  , "noimage")';
					$reqnoimg = $connexion->prepare($sqlnoimg);
					$reqnoimg->execute($qnoimg);
					$reqnoimg->closeCursor();
				}
			header('Location:acceuil.php');
			//fin image
	}
	else {
		echo "saisisser un nombre dans le prix";
	}
	
}

?>
<div class ="form_inscription">
<div class="error"><?php if(isset($error_captcha)){ echo $error_captcha; } ?></div>
<div class="error"><?php if(isset($error_actif)){ echo $error_actif; } ?></div>
<div class="error"><?php if(isset($error_unknown)){ echo $error_unknown; } ?></div>

<div class="error"><?php if(isset($error_dejainscrit)){ echo $error_dejainscrit; } ?></div>
<h2 class ="h2inscription">Ajouter un appartement</h2><br/>
<form method="post" action="DeposerAppartement.php">
					
<span class="td_index"><label for="adresse">Adresse : </label></span>
<span class="mail_pad"><input type="text" name="adresse" size="30" maxlength="50" placeholder="Nom et num&eacute;ro rue de l'appartement" required/></span><br/><br/>

<span class="td_index"><label for="ville">Ville : </label></span>
<span class="mail_pad"><input type="text" name="ville" size="30" maxlength="50" placeholder="ex: Angers" required/></span><br/><br/>

<span class="td_index"><label for="codepostal">Code Postal : </label></span>
<span class="mail_pad"><input type="text" name="codepostal" size="30" maxlength="50" placeholder="ex: 49000" required/></span><br/><br/>

<span class="td_index"><label for="description_envirronement">Titre annonce :</label></span>
<span class="mail_pad"><input type="text" name="description_envirronement" size="30" maxlength="50" placeholder="ex : Appartement proche commerce" required/></span><br/><br/>

<span class="td_index"><label for="typeH">Type d'habitation : </label></span>
<span class="classe_pad"><select name="typeH" size="1"></span><br/><br/>
<?php
 
 if ( !$TypeHabitation)
{
	echo "pb";
}
else
{
	foreach ($TypeHabitation as $OneRow)
	{ //on parcourt toutes les lignes une par une	

		echo "<option value=$OneRow[0] >$OneRow[0]</option>"; 
	}
} 
$TypeHabitation=null;
			
?>
</select><br><br>

<span class="td_index"><label for="piece">Nombre de pi&egrave;ce(s) : </label></span>
<span class="classe_pad"><select name="piece">
<?php 
 

for ($i = 1; $i <= 10; $i ++) 
{ 
	echo '<option value="'.$i.'">'.$i.'</option>'; 
} 

?> 

</select></span><br/><br/>

                    
<span class="td_index"><label for="surface">Surface : </label></span>
<span class="classe_pad"><select name="surface">
<?php 
 

for ($i = 9; $i <= 50; $i ++) 
{ 
	echo '<option value="'.$i.'">'.$i.'</option>'; 
} 

?> 

</select></span> M&sup2<br/><br/>

<span class="td_index"><label for="distance">Distance du lyc&eacute;e Chevrollier : </label></span>
<span class="classe_pad"><select name="distance" size="1"></span><br/><br/>
<?php

for ($i = 50; $i <= 60000; $i += 50) 
{ 
echo '<option value="'.$i.'">'.$i.'</option>'; 
} 
			
?>
</select></span> Metres<br/><br/>

<span class="td_index"><label for="prix">Prix du loyer : </label></span>
<span class="prenom_pad"><input type="text" name="prix" size="30" maxlength="50" placeholder="ex: 300" required/></span>euros<br/><br/>
<span class="td_index"><label for="meubler">Meubler :</label>
								<input type="radio" name="meubler" value="1">Oui
     							<input type="radio" name="meubler" value="2">Non</span><br/><br/>
<span class="td_index"><label for="Description">Description : </label></span><br>
<span class="tel_pad"><textarea name="Description" rows="10" cols="50"  placeholder="Decrivez votre habitation.."></textarea></span><br/><br/>

<label for="file">Uploader photo:</label>

<input type="file" accept=".jpg" name="file[]" multiple="multiple" id="file"><br>

<span class="submit_pad"><input type="submit" value="Ajouter"/></span>
</div>
</form>
<?php 
$site->footer();